import sys
import os
import random
# from PySide6.QtGui import QGuiApplication
from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, QTimer
from chart_data_source import ChartDataSource

class Test(QObject):
    def __init__(self, parent: QObject) -> None:
        super().__init__(parent)
        self._data_source = ChartDataSource(self)
        self._x = 0
        self._timer = QTimer(self)
        self._timer.timeout.connect(self._gen_data)

    def _gen_data(self):
        x = self._x
        self._x += 1
        y = random.random() * 10
        self._data_source.add_data(x, y)
        
    def get_data_source(self):
        return self._data_source
    
    def start_gen_data(self):
        self._timer.start(1)
        

if __name__ == "__main__":
    print("hello")
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    current_dir = os.path.dirname(os.path.abspath(__file__))
    test = Test(None)
    dataSource = test.get_data_source()
    test.start_gen_data()
    engine.setInitialProperties({"dataSource": dataSource})
    engine.load(current_dir + "/main.qml")
    if not engine.rootObjects():
        print("hello")
        sys.exit(-1)
    sys.exit(app.exec())
